﻿using System;
using System.Collections;
using System.Collections.Generic;
using tool;
using UnityEngine;
using Random = System.Random;

public class Cube : MonoBehaviour
{
    private List<Pattern> patterns = new List<Pattern>(); //四个形状

    private int patternIndex = 0;

    public Vector2 position; //世界坐标

    //  public bool isControll ; //没有在控制
    Coroutine downPattern;

    private void Start()
    {
        Init();
        Draw();


       // Invoke(nameof(Down), 1);
    }

    public float currentTime = 0;

    private void Update()
    {
    
        if (TimerManager.CheckTimeUp("bbb",Time.deltaTime,2f))
        {
            Debug.LogError("bbb");
        }
    }


    void Init()
    {
        patterns.Clear();
        CreatePatternCube();
    }

    void CreatePatternCube()
    {
        System.Random random = new Random();
        int number = random.Next(1, 8);
     //   Debug.Log("本次的数字为  " + number + "***************");
        switch (number)
        {
            case 1:
                PatternLLL();
                break;
            case 2:
                PatternLine();
                break;
            case 3:
                PatternMirrorL();
                break;
            case 4:
                PatternSquare();
                break;
            case 5:
                PatternZZZ();
                break;
            case 6:
                PatternSoil();
                break;
            case 7:
                PatternMirrorZ();
                break;
        }
    }

    #region 7 种俄罗斯方块的形状坐标

    void PatternLLL() //正 L索引 1
    {
        Pattern pattern1 = new Pattern();
        pattern1.positionList.Add(new Vector2(-1, -1));
        pattern1.positionList.Add(new Vector2(0, -1));
        pattern1.positionList.Add(new Vector2(-1, 0));
        pattern1.positionList.Add(new Vector2(-1, 1));
        patterns.Add(pattern1);

        Pattern pattern2 = new Pattern();
        pattern2.positionList.Add(new Vector2(0, 1));
        pattern2.positionList.Add(new Vector2(1, 1));
        pattern2.positionList.Add(new Vector2(1, 0));
        pattern2.positionList.Add(new Vector2(1, -1));
        patterns.Add(pattern2);

        Pattern pattern3 = new Pattern();
        pattern3.positionList.Add(new Vector2(-1, 1));
        pattern3.positionList.Add(new Vector2(0, 1));
        pattern3.positionList.Add(new Vector2(1, 1));
        pattern3.positionList.Add(new Vector2(-1, 0));
        patterns.Add(pattern3);

        Pattern pattern4 = new Pattern();
        pattern4.positionList.Add(new Vector2(1, 0));
        pattern4.positionList.Add(new Vector2(-1, -1));
        pattern4.positionList.Add(new Vector2(0, -1));
        pattern4.positionList.Add(new Vector2(1, -1));
        patterns.Add(pattern4);
    }

    void PatternLine() //横线/竖线 索引 2
    {
        Pattern pattern1 = new Pattern();
        pattern1.positionList.Add(new Vector2(0, 0));
        pattern1.positionList.Add(new Vector2(1, 0));
        pattern1.positionList.Add(new Vector2(2, 0));
        pattern1.positionList.Add(new Vector2(3, 0));
        patterns.Add(pattern1);

        Pattern pattern2 = new Pattern();
        pattern2.positionList.Add(new Vector2(0, -1));
        pattern2.positionList.Add(new Vector2(0, 0));
        pattern2.positionList.Add(new Vector2(0, 1));
        pattern2.positionList.Add(new Vector2(0, -2));
        patterns.Add(pattern2);
    }

    void PatternMirrorL() //镜像 L索引 3
    {
        Pattern pattern1 = new Pattern();
        pattern1.positionList.Add(new Vector2(0, 0));
        pattern1.positionList.Add(new Vector2(1, 0));
        pattern1.positionList.Add(new Vector2(1, 1));
        pattern1.positionList.Add(new Vector2(1, 2));
        patterns.Add(pattern1);

        Pattern pattern2 = new Pattern();
        pattern2.positionList.Add(new Vector2(0, 1));
        pattern2.positionList.Add(new Vector2(1, 1));
        pattern2.positionList.Add(new Vector2(2, 0));
        pattern2.positionList.Add(new Vector2(2, 1));
        patterns.Add(pattern2);

        Pattern pattern3 = new Pattern();
        pattern3.positionList.Add(new Vector2(0, 0));
        pattern3.positionList.Add(new Vector2(0, 1));
        pattern3.positionList.Add(new Vector2(0, 2));
        pattern3.positionList.Add(new Vector2(1, 2));
        patterns.Add(pattern3);

        Pattern pattern4 = new Pattern();
        pattern4.positionList.Add(new Vector2(0, 0));
        pattern4.positionList.Add(new Vector2(1, 0));
        pattern4.positionList.Add(new Vector2(2, 0));
        pattern4.positionList.Add(new Vector2(0, 1));
        patterns.Add(pattern4);
    }

    void PatternSquare() //田字格形状 索引 4
    {
        Pattern pattern1 = new Pattern();
        pattern1.positionList.Add(new Vector2(0, 1));
        pattern1.positionList.Add(new Vector2(0, 0));
        pattern1.positionList.Add(new Vector2(1, 0));
        pattern1.positionList.Add(new Vector2(1, 1));
        patterns.Add(pattern1);

        Pattern pattern2 = new Pattern();
        pattern2.positionList.Add(new Vector2(0, 1));
        pattern2.positionList.Add(new Vector2(0, 0));
        pattern2.positionList.Add(new Vector2(1, 0));
        pattern2.positionList.Add(new Vector2(1, 1));
        patterns.Add(pattern2);
    }

    void PatternZZZ() //正 Z 索引 5
    {
        Pattern pattern1 = new Pattern();
        pattern1.positionList.Add(new Vector2(0, 1));
        pattern1.positionList.Add(new Vector2(1, 1));
        pattern1.positionList.Add(new Vector2(1, 0));
        pattern1.positionList.Add(new Vector2(2, 0));
        patterns.Add(pattern1);

        Pattern pattern2 = new Pattern();
        pattern2.positionList.Add(new Vector2(0, 0));
        pattern2.positionList.Add(new Vector2(0, -1));
        pattern2.positionList.Add(new Vector2(1, 0));
        pattern2.positionList.Add(new Vector2(1, 1));
        patterns.Add(pattern2);
    }

    void PatternSoil() //土子形状 索引 6
    {
        Pattern pattern1 = new Pattern();
        pattern1.positionList.Add(new Vector2(0, 0));
        pattern1.positionList.Add(new Vector2(1, 0));
        pattern1.positionList.Add(new Vector2(2, 0));
        pattern1.positionList.Add(new Vector2(1, 1));
        patterns.Add(pattern1);

        Pattern pattern2 = new Pattern();
        pattern2.positionList.Add(new Vector2(0, 1));
        pattern2.positionList.Add(new Vector2(1, 1));
        pattern2.positionList.Add(new Vector2(1, 0));
        pattern2.positionList.Add(new Vector2(1, 2));
        patterns.Add(pattern2);

        Pattern pattern3 = new Pattern();
        pattern3.positionList.Add(new Vector2(0, 0));
        pattern3.positionList.Add(new Vector2(1, 0));
        pattern3.positionList.Add(new Vector2(2, 0));
        pattern3.positionList.Add(new Vector2(1, -1));
        patterns.Add(pattern3);

        Pattern pattern4 = new Pattern();
        pattern4.positionList.Add(new Vector2(1, 0));
        pattern4.positionList.Add(new Vector2(1, 1));
        pattern4.positionList.Add(new Vector2(1, 2));
        pattern4.positionList.Add(new Vector2(2, 1));
        patterns.Add(pattern4);
    }

    void PatternMirrorZ() //镜像 Z索引 7
    {
        Pattern pattern1 = new Pattern();
        pattern1.positionList.Add(new Vector2(0, 0));
        pattern1.positionList.Add(new Vector2(1, 0));
        pattern1.positionList.Add(new Vector2(1, 1));
        pattern1.positionList.Add(new Vector2(2, 1));
        patterns.Add(pattern1);

        Pattern pattern2 = new Pattern();
        pattern2.positionList.Add(new Vector2(0, 1));
        pattern2.positionList.Add(new Vector2(0, 2));
        pattern2.positionList.Add(new Vector2(1, 0));
        pattern2.positionList.Add(new Vector2(1, 1));
        patterns.Add(pattern2);
    }

    #endregion

    #region 旋转，是否超边检测

    public void Rotation()
    {
        int currentIndex = patternIndex;
        int targetIndex = patternIndex;

        if (currentIndex < patterns.Count - 1)
        {
            targetIndex += 1;
        }
        else
        {
            targetIndex = 0;
        }

        foreach (var cellPosition in patterns[targetIndex].positionList)
        {
            Vector2 targetPosition = cellPosition + position;
            //判断当前的 Cube 目标的位置是否出格子，出格子了，return
            if (targetPosition.x <= 0 || targetPosition.x >= 9 || targetPosition.y < 0)
            {
                return;
            }


            Cell targetCell = GameManager.Instance.board.GetCell(targetPosition);

            if (targetCell != null)
            {
                //判断当前 Cube目标位置是否有 state为 fixed的，如果有，return
                if (targetCell.state == CellState.Fixed)
                {
                    return;
                }
            }
        }

        Clear();
        patternIndex = targetIndex;
        Draw();
    }

    public bool OutOfEdgeLeft()
    {
        foreach (Vector2 patterPosition in patterns[patternIndex].positionList)
        {
            if (patterPosition.x + position.x <= 0)
            {
                return true;
            }
        }

        return false;
    }

    public bool OutOfEdgeRight()
    {
        foreach (Vector2 patterPosition in patterns[patternIndex].positionList)
        {
            if (patterPosition.x + position.x >= 9)
            {
                return true;
            }
        }

        return false;
    }

    public bool OutOfEdgeDown() //有没有到底
    {
        foreach (Vector2 patterPosition in patterns[patternIndex].positionList)
        {
            Vector2 targetPosition = patterPosition + position + Vector2.down;
            if (targetPosition.y < 0)
            {
                return true;
            }
        }

        return false;
    }

    public bool OverrideFixed(Vector2 offset) //有没有覆盖固定块
    {
        foreach (Vector2 patterPosition in patterns[patternIndex].positionList)
        {
            Vector2 targetPosition = patterPosition + position + offset;

            Cell targetCell = GameManager.Instance.board.GetCell(targetPosition);

            if (targetCell != null)
            {
                if (targetCell.state == CellState.Fixed)
                {
                    return true;
                }
            }
        }

        return false;
    }

    public void Left()
    {
        if (OutOfEdgeLeft() || OverrideFixed(Vector2.left))
        {
            return;
        }

        Clear();
        position += Vector2.left;
        Draw();
    }

    public void Right()
    {
        if (OutOfEdgeRight() || OverrideFixed(Vector2.right))
        {
            return;
        }

        Clear();
        position += Vector2.right;
        Draw();
    }

    public void Fixed()
    {
        foreach (var patternPosition in patterns[patternIndex].positionList)
        {
            Vector2 cellPosition = patternPosition + position;

            Cell targetCell = GameManager.Instance.board.GetCell(cellPosition);

            if (targetCell != null)
            {
                targetCell.state = CellState.Fixed;
            }
        }
    }

    #endregion


    public void Down()
    {
        if (OutOfEdgeDown() || OverrideFixed(Vector2.down)) //没有超过地板，或者下面没有固定块时候
        {
            GameManager.Instance.CreateCube();
            Fixed();
            GameManager.Instance.board.DoDestory(); //检测是否有一行满了，满了则删除
            GameObject.Destroy(gameObject);
            return;
        }

        Clear();
        position += Vector2.down;
        Draw();
        // currentTime = 0;
        
       // CancelInvoke(nameof(Down));
       // Invoke(nameof(Down), 1);
    }

    public void Draw()
    {
        //根据当前的花纹中的位置列表，分别计算出对应的board上的坐标。用这个坐标找到board上的cell
        //调用cell的 show
        foreach (var patternPosition in patterns[patternIndex].positionList)
        {
            Vector2 cellPosition = patternPosition + position;


            Cell targetCell = GameManager.Instance.board.GetCell(cellPosition);

            if (targetCell != null)
            {
                targetCell.state = CellState.Cube;
            }
        }
    }

    public void Clear()
    {
        //根据当前的花纹中的位置列表，分别计算出对应的board上的坐标。用这个坐标找到board上的cell
        //调用cell的 show
        foreach (var patternPosition in patterns[patternIndex].positionList)
        {
            Vector2 cellPosition = patternPosition + position;

            Cell targetCell = GameManager.Instance.board.GetCell(cellPosition);

            if (targetCell != null)
            {
                targetCell.state = CellState.None;
            }
        }
    }
}